package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.sky.result.Result;

import java.util.List;

/**
 * @author 贾育权~
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/setmeal")

//就是抑制警告
@SuppressWarnings("all")
public class setMealController {// 套餐管理的
    @Autowired
    private SetMealService setMealService;// 注入



    // 新增套餐
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @RequestMapping
    public Result save(@RequestBody SetmealDTO setMealDTO){
        // 前端传过来的数据//用一个实体对象来接收用我们
        setMealService.saveWithDish(setMealDTO);


       return  Result.success();
    }
    //分页查询
    @RequestMapping("/page")

    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        // 前端传过来的数据
       PageResult pageResult=setMealService.pageQuery(setmealPageQueryDTO);
       return Result.success(pageResult);
    }
//删除套餐
    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result delete(@RequestParam List<Long> ids) {//我们用一个集合去接收参数
        setMealService.deleteById(ids);
        return Result.success();

    }
    // 根据id查询套餐//也可以用于页面的回显的操作
    @Cacheable(cacheNames ="setmealCache" ,key = "#categoryId")//把方法的返回值加入到缓存中
    //对应的key 就是setmealCache::100//就是这样Key 的类型对应的值就是返回值
    @GetMapping("/{id}")
    public Result<SetmealDTO> getById(@PathVariable Long id){//路径参数 去查询套餐
        SetmealDTO setmealDTO=setMealService.getByIdWithDish(id);
        return Result.success(setmealDTO);//返回数据 给前端 要的信息给他
    }


    //根据id 删除套餐
    //@GetMapping("/list")
   // @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @DeleteMapping("/{id}")
  //  @ApiOperation("根据id删除套餐")
    //@Cacheable(cacheNames ="setmealCache" ,key = "#categoryId")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteById(@PathVariable Long id){
        setMealService.deleteById(id);
        return Result.success();
    }
    // 修改套餐1.数据的回显操作{查询一些东西啊}{查询多个表}
    //我们是不是还有去覆盖之前的套餐的内容，而且更改好多表的相关的数据
    /**
     * 根据id查询套餐，用于修改页面回显数据
     *
     * @param id
     * @return
     */
//    @GetMapping("/{id}")
//    @ApiOperation("根据id查询套餐")
//    public Result<SetmealVO> getById(@PathVariable Long id) {
//        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
//        return Result.success(setmealVO);
//    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
       setMealService.update(setmealDTO);
       return Result.success();


    }
    //起售与停售卖
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
//    public Result startOrStop(@PathVariable Integer status,Long id) {
//        setMealService.startOrStop(status,id);
//        return Result.success();
//    }
    public Result startOrStop(@PathVariable Integer status,Long id){//启用禁用套餐
        setMealService.startOrStop(status,id);
        return Result.success();



    }



}
